#include "ocr.h"
#include "ScreenGrab.hpp"
#include "tiku.hpp"
#include <string>
#include <map>
#include <cstdio>
using namespace std;
string AppID = "119050963";
string API_Key = "vwSItpg3QXDY7Vg8fmSfTwWK";
string Secret_Key = "7J0v2bmaZzjJDwTzQPHpOaaCUO1osF9G";
map<string, string> options;
aip::Ocr ocr(AppID, API_Key, Secret_Key);
Tiku tk("tiku.db");
ScreenGrab sg;

inline string read_text(Json::Value response) {
    string ret = "";
    for (auto words : response["words_result"]) {
        // cout << words["words"].asString();
        ret += words["words"].asString();
    }
    return ret;
}

void help() {
    cout << "q键退出上一次操作;" << endl;
    cout << "进入答题后, enter键截图" << endl;
}

void menu() {
    cout << "咸鱼答题助手" << endl;
    cout << "************************" << endl;
    cout << "1.开始答题"  << endl;
    cout << "2.查看题库" << endl;
    cout << "3.插入新题" << endl;
    cout << "4.帮助" << endl;
    cout << "5.退出" << endl;
    cout << "************************" << endl;
}

int main() {
    system("chcp 65001");
    // menu();
    options["language_type"] = "CHN_ENG";
    system("cls");
    while (1) {
        menu();
        string xx;
        // cin >> x;
        // cout << x << endl;
        // scanf("%d", &x);
        getline(cin, xx);
        int x = atoi(xx.c_str());
        switch (x) {
            case 1:
            {
                system("cls");
                while (1) {
                    cout << "是否截取当前题目" << endl;
                    string op;
                    getline(cin, op);
                    if (op.compare(0, 1, "q") == 0) {
                        break;
                    }
                    sg.screenCapture(L"1.bmp", 0, 118, 334, 103);
                    cout << endl;
                    Json::Value response = ocr.general_basic("./1.bmp", options);
                    cout << response << endl;
                    string text = read_text(response);
                    cout << "question: " << text << endl;
                    tk.search(text);
                }
                break;
            }
            case 2:
            {
                system("cls");
                tk.show();
                while (1) {
                    string op;
                    getline(cin, op);
                    if (op.compare(0, 1, "q") == 0) {
                        break;
                    }
                }
                system("cls");
                break;
            }
            case 3:
            {
                system("cls");
                while (1) {
                    string q;
                    string a;
                    cout << "题目:";
                    cin >> q;
                    if (q.compare(0, 1, "q") == 0) {
                        break;
                    }
                    cout << "答案";
                    cin >> a;
                    if (a.compare(0, 1, "q") == 0) {
                        break;
                    }
                    tk.insert_v2(q, a);
                }
                system("cls");
                break;
            }
            case 4:
            {
                system("cls");
                help();
                while (1) {
                    string op;
                    getline(cin, op);
                    if (op.compare(0, 1, "q") == 0) {
                        break;
                    }
                }
                break;
            }
            case 5:
            {
                exit(0);
            }
            default:
                break;
        }
    }
}
